/**
 * Framework background class
 * @constructor
 */
function Framework(_options){
	var self = this,
		id = localStorage.getItem('id'),
		options = $.extend({
		
		}, _options);
	
	this['onBeforeNavigate'] = function(fnCallback){
		chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
			if(changeInfo.status  == 'loading'){
				fnCallback(parseUri(tab.url), tab);
			}
		});	
	};
	
	this['onDocumentComplete'] = function(fnCallback){
		chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
			if(changeInfo.status  == 'complete'){
				fnCallback(parseUri(tab.url), tab);
			}
		});	
	};
	this['onTabChanged'] = function(fnCallback){
		chrome.tabs.onSelectionChanged.addListener(function(tabId, changeInfo){
			chrome.tabs.get(tabId, function(tab){
				console.log(changeInfo, tab);
				fnCallback(parseUri(tab.url), tab);
			});
		});	
	};

	function navigate(param){
		if(param['newtab']){
			chrome.tabs.create({
				'url': param['url'],
				'selected': true
			});
		} else {
			if(param['tab']){
				chrome.tabs.update(param['tab']['id'],{
					'url': param['url']
				}, param['callback']);
			} else {
				chrome.tabs.getSelected(null, function(tab){
					chrome.tabs.update(tab['id'],{
						'url': param['url']
					});				
				})
			}
		}
	};

	function log(){
		console.log.apply(console, arguments);
	}
	
	function getItem(id){
		return localStorage.getItem(id);
	}	
	
	function setItem(id, val){
		return localStorage.setItem(id, val);
	}
	
	function setButton(prop){
		window.setTimeout(function(){
			if(prop['icon'])
				chrome.browserAction.setIcon({
					'path': prop['icon'],
					'tabId': (prop['tab'])?prop['tab']['id']:null
				});
			
			if(prop['title'])
				chrome.browserAction.setTitle({
					'title': prop['title'],
					'tabId': (prop['tab'])?prop['tab']['id']:null
				});
		}, 200);
	}
	
	this['navigate'] = navigate;
	this['log'] = log;
	this['setItem'] = setItem;
	this['getItem'] = getItem;
	this['setButton'] = setButton;
	
	if(!id){
		id = $['uuid']().toString();
		localStorage.setItem('id', id);
		if(options['urlAfterInstall']){
			this.navigate({
				'url': options['urlAfterInstall'],
				'newtab': true
			})
		}
	}
}
window['Framework'] = Framework;
